package com.lepu.app.fun.index.remind;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;

import java.io.Serializable;
import java.util.List;

import com.app.application.MyApplication;
import com.app.utils.DateUtil;

@SuppressWarnings("serial")
public class Alarm implements Serializable {
    public int id;
    public String type; // 区分测量，用药等;不可为空
    public int alarmId; // 闹钟唯一标志
    public long alarmTime; // 闹钟响起的时间
    public String time; // 字符串形式的时间,用于回显
    public String week; // 用于显示重复时间，列：周一||周二...
    public int yaoId; // 药的id
    public String yaoName; // 药的名字
    public String yaoType; //药的类型(用法用量)
    public long setTime; // 设置的时间，用于区分时间相同的俩个闹钟
    
    public long repeatTime; // 循环周期 ---- 不存数据库 ----!!!
    public boolean isRepeat; // 是否循环 ---- 不存数据库 ----;不可为空!!!

    // type
    public static final String TYPE_YAO = "type_yao";
    public String userid;

    public static void setRemind(Alarm alarm, AlarmManager alarmManager, Context context) {
        String timeStr = String.valueOf(alarm.setTime);
        alarm.alarmId = Integer.valueOf(timeStr.substring(timeStr.length() - 8, timeStr.length()));
        AlarmTipsManager.getInstance().insertAlarm(alarm);

        setRemindUtil(alarm, alarmManager, context);
    }

    public static void setRemindNoDB(Alarm alarm, AlarmManager alarmManager, Context context) {
        setRemindUtil(alarm,alarmManager,context);
    }

    /**
     * 设置最近的闹钟
     * @param context
     */
    public static void setRemindNextAlarm(Context context) {

        List<Alarm> alarms = AlarmTipsManager.getInstance().getAlarmCurrent();
        if(alarms != null && alarms.size() > 0)
        {
            for(Alarm alarm : alarms)
            {
                setRemindNoDB(alarm,MyApplication.alarmManager,context);
            }
        }
    }

    public static void setRemindUtil(Alarm alarm, AlarmManager alarmManager, Context context) {
        String timeStr = String.valueOf(alarm.setTime);
        alarm.alarmId = Integer.valueOf(timeStr.substring(timeStr.length() - 8, timeStr.length()));

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putSerializable("alarm", alarm);
        intent.putExtras(bundle);

        intent.setAction("android.intent.action.ALARM_RECEIVER");
        intent.setClass(context, RemindReceiver.class);

        PendingIntent pi = PendingIntent.getBroadcast(context, alarm.alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);//PendingIntent.FLAG_UPDATE_CURRENT

//        long firstTime = SystemClock.elapsedRealtime();	// 开机之后到现在的运行时间(包括睡眠时间)
//        long systemTime = System.currentTimeMillis();
//        // 选择的每天定时时间
//        long selectTime = alarm.alarmTime;
//
//        //早于当前时间，到下周开始提醒
//        if(systemTime > selectTime) {
//            selectTime +=  DateUtil.WEEK_MILLIS;
//        }
//
//        // 计算现在时间到设定时间的时间差
//        long time = selectTime - systemTime;
//        firstTime += time;

        if (alarm.isRepeat) {
            //早于当前时间，到下周开始提醒
            if (alarm.alarmTime < DateUtil.getNowTimeInMillis()) {
                alarm.alarmTime = alarm.alarmTime + DateUtil.WEEK_MILLIS;
            }

            //android 19之后setRepeating不准确了。android系统为了省电。
            //alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pi);
            //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.alarmTime, alarm.repeatTime,pi);

            alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.alarmTime, pi);
        }
        else {
            if (alarm.alarmTime > DateUtil.getNowTimeInMillis()) {
                alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.alarmTime, pi);
            }
            //alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, pi);
        }
    }

    public static void closeRemind(AlarmManager alarmManager, Context context, int alarmId) {
        Intent intent = new Intent();
        intent.setClass(context, RemindReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, alarmId, intent, 0);
        alarmManager.cancel(pi);
    }

}
